/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    loadList('/v1/manager/template/list.json');

    $('#delete-template-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        $(this).find('.template-id').val(id);
        $(this).find('.template-name').text(name);
    });
    $('#delete-template-dialog').on('hide.bs.modal', function(){
        $(this).find('.template-id').val('');
        $(this).find('.template-name').text('');
    });

    $('#delete-template-dialog .btn-save').on('click', function(){
        var id = $('#delete-template-dialog').find('.template-id').val();
        sendAjax('/v1/manager/delete/template', 'post', {id:id}, function(json){
            $('#delete-template-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url) {
    $("#template-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: '<a class="btn btn-success edit-template" href="/manager/contract/template/create.html">添加</a>',
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'name',
            title: ' 模板名称'
        }, {
            field: 'created_at',
            title: ' 创建时间'
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-template" href="/manager/contract/template/view/'+value+'.html">详情</a>';
                action += '<a class="btn btn-xs btn-success edit-template" href="/manager/contract/template/edit/'+row.id+'.html">编辑</a>';
                action += '<a class="btn btn-xs btn-danger delete-template" data-toggle="modal" data-target="#delete-template-dialog" data-id="'+row.id+'" data-name="'+row.name+'">删除</a>';
                return action;
            }
        }]
    });
}